
/**
 * Created by wurenjie on 15/12/17.
 */
var __ROOT__ = 'http://localhost/shu_ces_wechat';


$(function() {
    var article = {
        obj: {
            pullUpEl: $("#pullUp"),
            pullUpOffset: $("#pullUp").height(),
            generatedCount: 0
        },
        init: function () {
            var myScroll;
            document.addEventListener('load',article.loaded(),false);
            article.choose("0", 1);
        },
        loaded: function(){
            pullUpEl = document.getElementById('pullUp');
            setTimeout(function(){
                myScroll = new iScroll('list',{
                    scrollbarClass: 'myScrollbar',
                    useTransition: false,
                    onRefresh: function(){
                        if (pullUpEl.className.match('loading')) {
                            pullUpEl.className = '';
                            pullUpEl.querySelector('a').innerHTML = '上拉加载更多';
                        }
                    },
                    onScrollMove: function(){
                        //阻止浏览器默认事件
                        document.addEventListener('touchmove', function(e) {
                            e.preventDefault();
                        }, false);
                        if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                            pullUpEl.className = 'flip';
                            pullUpEl.querySelector('a').innerHTML = '释放加载';
                            this.maxScrollY = this.maxScrollY;
                        } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                            pullUpEl.className = '';
                            pullUpEl.querySelector('a').innerHTML = '查看更多';
                            this.maxScrollY = pullUpOffset;
                        }
                    },
                    onScrollEnd: function(){
                        if (pullUpEl.className.match('flip')) {
                            pullUpEl.className = 'loading';
                            pullUpEl.querySelector('a').innerHTML = '<div class="i_loading_small"></div>';
                            article.pullUpAction();
                        }
                    }
                });
            },100)
        },
        pullUpAction: function() {
            setimeout = setTimeout(function() {
                if(page-1<$('.p_totalpage').text()){
                    $('.p_prepage').text(page);
                }
                //ajax加载列表
                article.loadlist(page,function(data){
                    page++;
                    //var data = data.data;
                    var html = '';
                    $('.i_loading').remove();
                    if(data.count==0){
                        return false;
                    }else{
                        if(data.data.length==0){
                            page--;
                            $('#pullUp a').text('无更多');
                            return false;
                        }else if (data.data.length > 0) {
                            html = juicer(list_tpl, data);
                            dom.append(html);
                            myScroll.refresh();
                        }
                        var img = $('img');
                        var index = 0;
                        for(var i=0; i<img.length;i++){
                            img[i].onload = function(){
                                myScroll.refresh();
                            }
                        }
                    }
                });
            }, 100);
        },
        loadlist : function(page) {
            var client = new HproseHttpClient(__ROOT__+'/hprose.php/Article', ['getArticleList']);

            client.getArticleList(type, page, function (result) {
                result = JSON.parse(result);
                if (result.success == 1) {
                    var tpl = '';
                    $.each(result.result, function (i, item) {
                        tpl +=  '<li class="list-item">' ;
                        tpl +=  '<a href="{:U(\'Article/articleDetail\')}/aid/' + item.id + '">' ;
                        tpl +=  '<div class="title">' + item.title + '</div>' ;
                        tpl +=  '<div class="itd">' ;
                        tpl +=  '<span class="kind">[' + item.typename + ']</span>' ;
                        tpl +=  '<span class="createTime">'+(item.createtime).substring(0,10)+'</span>' ;
                        tpl +=  '</div>' ;
                        tpl +=  '</a>' ;
                        tpl +=  '</li>';
                    });
                    $("#list").html(tpl);
                    $("#type").val(type);
                    //pagecount(page, result.count);
                }
            })
        },
        choose: function (type, page) {

            var client = new HproseHttpClient(__ROOT__+'/hprose.php/Article', ['getArticleList']);

            client.getArticleList(type, page, function (result) {
                result = JSON.parse(result);
                //    console.log(result);
                if (result.success == 1) {
                    var tpl = '';
                    $.each(result.result, function (i, item) {
                        tpl += '<li class="list-item">' +
                            '<a href="'+__ROOT__+'/Article/articleDetail/aid/' + item.id + '">' +
                            '<div class="title">' + item.title + '</div>' +
                            '<div class="itd">' +
                            '<span class="kind">[' + item.typename + ']</span>' +
                            '<span class="createTime">' + (item.createtime).substring(0,10) + '</span>' +
                            '</div>' +
                            '</a>' +
                            '</li>';
                    });
                    $("#list").html(tpl);

                    $("#type").val(type);
                    //pagecount(page, result.count);
                }
            })
        },
        pagecount: function (page, count) {
            var p = '';
            p += '共' + count + '条记录';
            var maxpage = Math.ceil(count / 10);
            if (count != 0) {
                var j = 0;
                if (page <= 3) {
                    for (j = 1; j <= (5 < maxpage ? 5 : maxpage); j++) {
                        if (j == page)
                            p += '&nbsp;&nbsp;' + j;
                        else
                            p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + j + ',' + count + ')">' + j + '</a>';
                    }
                    if (maxpage == j)
                        p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(6,' + count + ')">6</a>';
                    else {
                        if (page + 10 < maxpage) {
                            p += '&nbsp;&nbsp;……';
                            p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + (page + 10) + ',' + count + ')">>></a>';
                        }
                        p += '&nbsp;&nbsp;……';
                        p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + maxpage + ',' + count + ')">' + maxpage + '</a>';
                    }
                }
                else if (page >= maxpage - 2) {
                    p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(1,' + count + ')">1</a>';
                    if (maxpage - 5 > 1)
                        p += '&nbsp;&nbsp;……';
                    if (page - 10 > 1) {
                        p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + (page - 10) + ',' + count + ')"><<</a>';
                        p += '&nbsp;&nbsp;……';
                    }
                    for (j = (maxpage - 5 >= 2 ? (maxpage - 5) : 2); j <= maxpage; j++) {
                        if (j == page)
                            p += '&nbsp;&nbsp;' + j;
                        else
                            p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + j + ',' + count + ')">' + j + '</a>';
                    }
                }
                else {
                    p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(1,' + count + ')">1</a>';
                    if (2 != page - 2)
                        p += '&nbsp;&nbsp;……';
                    if (page - 10 > 1) {
                        p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + (page - 10) + ',' + count + ')"><<</a>';
                        p += '&nbsp;&nbsp;……';
                    }
                    for (j = (page - 2); j <= (page + 2); j++) {
                        if (j == page)
                            p += '&nbsp;&nbsp;' + j;
                        else
                            p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + j + ',' + count + ')">' + j + '</a>';
                    }
                    if (maxpage != j)
                        p += '&nbsp;&nbsp;……';
                    if (page + 10 < maxpage) {
                        p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + (page + 10) + ',' + count + ')">>></a>';
                        p += '&nbsp;&nbsp;……';
                    }
                    p += '&nbsp;&nbsp;<a class="hand" onclick="pagechange(' + maxpage + ',' + count + ')">' + maxpage + '</a>';
                }
            }
            $("#page").html(p);
        },
        pagechange: function (page) {
            choose($("#type").val(), page);
        },



    }
    article.init();


});
